package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;


    //分页查询
    @Override
    public PageResult findPage(Integer pageNum, Integer sizeNum, String queryString) {
        PageHelper.startPage(pageNum,sizeNum);
        Page<Permission> page = permissionDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //新增权限
    @Override
    public void add(Permission permission) {
        permission.setCreateontime(new Date());
        permissionDao.add(permission);
    }

    //回显权限
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }


    //修改权限数据
    @Override
    public void update(Permission permission) {
        permissionDao.update(permission);
    }
    //删除用户
    @Override
    public void delete(Integer id) {
        //查询权限角色关联表中是否有该权限，有则提示删除失败
        Integer count = permissionDao.findPermissionAndRole(id);
        if(count>0){
            //说明存在关联关系，则不能删除
            throw new RuntimeException("该权限已存在角色中，不能删除！");
        }else{
            //删除权限信息
            permissionDao.delete(id);
        }

    }
}
